-- for循环,for后面的是初值,结束值,步长(不写默认是1,可以是负数) for i = 1, 10, 2do print(i) if i == 5thenbreakend end
-- 在for中不可以修改i,修改i相当于新建了一个变量 for i = 10, 1, -1do print("first:"..tostring(i)) -- 按顺序10到1 i = 5-- 相当于 local i = 5 print("local:"..tostring(i)) -- 重新新建了一个i,i一直是5 end
-- while循环 local n = 10 while n > 1do n = n - 1 print(n) end
-- for循环整个table local t = {"a", "b", "c", "d"} for i=1, #t do print(i, t[i]) end
-- lua文件可以返回值,同时require时,子lua程序会执行 local r = require("hello") print(r) -- 再次引入发现lua程序不执行,同时_G.count还是2,返回值一样可以接收 local c = require("hello") print("r:", r) print("c:", c)
-- _G.count还是2 print("count:", _G.count)
--[[ 输出结果如下: hello world two hello world r: done num:2 r: done num:2 c: done num:2 count: 2 ]]
-- table语法糖 local t = { a = 0, add = function(tab, sum) tab.a = tab.a + sum end } -- 相当于t.add(t, 10) t:add(10) print(t.a)
-- 封装
-- person.lua local Person = {}
-- self的元表 local SelfMt = { -- 设置 __index 为 Person,以便 self 可以访问 Person 中的方法,同时不存在的方法不能访问 __index = function(t, k) if Person[k] then return Person[k] else error("attempt to access a nil value or nil function") end end, -- 重写__newindex,当self中不存在该键时,不能赋值 __newindex = function(t, k, v) if Person[k] then rawset(t, k, v) else error("attempt to modify a read-only table") end end -- 其他需要扩展的可以自行扩展 -- ... }
functionPerson:new(name, age) localself = {} self.name = name self.age = age setmetatable(self, SelfMt) returnself end
functionPerson:sayHello() print("Hello, my name is " .. self.name .. " and I am " .. self.age .. " years old.") end
return Person
-- test.lua local Person = require("person")
local person = Person:new("John", 30) person:sayHello()
person.name = "Jane" person.age = 25
person:sayHello()
-- 异常:attempt to modify a read-only table -- person.bag = "bag" -- 异常:attempt to access a nil value or nil function -- person:goodbye()
-- 继承
-- person.lua 其他和 封装的 person.lua 一样,新加一个函数 functionPerson:sayName() print("My name is " .. self.name) end
-- student.lua local Person = require("person") -- 引入 Person 模块
local Student = {}
-- 重新设置元表(先找子类,再找父类) local SelfMt = { __index = function(t, k) if Student[k] then return Student[k] elseif Person[k] then return Person[k] else error("attempt to access a nil value or nil function") end end, __newindex = function(t, k, v) if Student[k] then rawset(t, k, v) elseif Person[k] then rawset(t, k, v) else error("attempt to modify a read-only table") end end -- 其他需要扩展的可以自行扩展 -- ... }
functionStudent:sayHello() -- 重写父类的方法 print("Hello, my name is " .. self.name .. ", I am " .. self.age .. " years old, and I'm in grade " .. self.grade .. ".") end
return Student
-- test.lua local Student = require("student")
local student = Student:new("Alice", 16, 10) -- sayHello() 被 Student 重写 student:sayHello() -- sayName() 继承自 Person student:sayName()
-- 继承父类 functionMetaManager:inherit(parent) table.insert(self.parents, parent) end
-- 重写 __index 查找方法 functionMetaManager:index(t, k) print(t, k) for _, parent inipairs(self.parents) do if parent[k] then return parent[k] end end
ifself.canSearchNil then returnnil else error("attempt to access a nil value or nil function") end end
-- 重写 __newindex 赋值方法 functionMetaManager:newindex(t, k, v) ifself.canModifyNil then rawset(t, k, v) return else for _, parent inipairs(self.parents) do if parent[k] then rawset(t, k, v) return end end error("attempt to modify a read-only table") end end
functionStudent:sayHello() print("Hello, my name is " .. self.name .. ", I am " .. self.age .. " years old, and I'm in grade " .. self.grade .. ".") end
return Student
-- test.lua(预计效果) local Person = require("person") -- local Student = require("student")
local person = Person:new("John", 20) person:sayHello() person:sayName()
// Start is called before the first frame update privatevoidStart() { // 创建LuaEnv luaEnv = new LuaEnv(); // 利用Lua执行cs代码 luaEnv.DoString("CS.UnityEngine.Debug.Log('Hello Lua')"); // 执行Lua代码 luaEnv.DoString("print('Hello World')"); }
local b = 10 functionaddB(one, two) return one + two + b end
person = { name = "john", age = 123 }
// 载入并使用变量 using System; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using XLua;
publicclassTestLua : MonoBehaviour { private LuaEnv luaEnv; privatevoidStart() { luaEnv = new LuaEnv(); luaEnv.DoString("require 'HelloWorld'"); // 载入全局变量 int a = luaEnv.Global.Get<int>("a"); string str = luaEnv.Global.Get<string>("str"); bool isRun = luaEnv.Global.Get<bool>("isRun");
Debug.Log(a); Debug.Log(str); Debug.Log(isRun);
// 获取Lua函数 var getBFunc = luaEnv.Global.Get<LuaFunction>("addB"); // 调用Lua函数并获取返回值,可以由此获得局部变量值(传递参数和参数类型唤醒函数) var result = getBFunc.Call(newobject[] { 1, 2 }, new Type[] { typeof(int), typeof(int) }); int sums = (int)result[0]; // 打印结果 Debug.Log(sums); // 应该输出: 10
// 获得全局表格 Person person = luaEnv.Global.Get<Person>("person"); Debug.Log(person.name + person.age.ToString()); }
privatevoidOnDestroy() { luaEnv.Dispose(); } }
classPerson { publicstring name; publicint age; }
6 接口映射
-- HelloWorld.lua.txt person = { name = "abc", age = 20, myPrint = function(self, a, b) print(a + b) CS.UnityEngine.Debug.Log("你好,世界") return a + b end }